package com.oa.common.result;

import lombok.Data;

/**
 * @author Admin
 * @title: Result
 * @projectName Cloud_OA_Office
 * @description: Result
 * @date 2024/10/1 20:17
 */
@Data
public class Result<T> {
    private Integer code; //状态码
    private String message; //返回信息
    private T data; // 定义泛型类

    //私有化构造
    private Result() {
    }
    //封装返回的数据
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        //封装数据
        if(body != null) {
            result.setData(body);
        }
        //设置状态码
        result.setCode(resultCodeEnum.getCode());
        //设置返回信息
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //无返回数据成功
    public static <T> Result<T> ok() {
        return build(null,ResultCodeEnum.SUCCESS);
    }
    //有返回数据成功
    public static <T> Result<T> ok(T data) {
        return build(data,ResultCodeEnum.SUCCESS);
    }

    //失败
    public static <T> Result<T> fail() {
        return build(null,ResultCodeEnum.FAIL);
    }
    public static <T> Result<T> fail(T data) {
        return build(data,ResultCodeEnum.FAIL);
    }
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
